﻿#include "ChargeWatchingDC.h"
#include "ui_chargewatchingDC.h"

ChargeWatchingDC::ChargeWatchingDC(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChargeWatchingDC)
{
    //<<<<<充电时间
    ui->setupUi(this);
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }
#if (GUN_AMOUNT == 1)
    ui->pushButton->hide();
#endif

    //    buttonDC1_1 = new QPushButton(this);
    //    buttonDC1_1->setParent(ui->tab1frame);
    //    buttonDC1_1->setGeometry(470,20,100,40);
    //    buttonDC1_1->setStyleSheet("QPushButton{border-style:none; background-image: url(:Res/image/unpressed.png);} QPushButton:pressed{background-image: url(:Res/image/pressed.png);}");
    //    buttonDC1_1->setFocusPolicy(Qt::NoFocus);
    //    buttonDC1_1->hide();

    //    buttonDC2_2 = new QPushButton(this);
    //    buttonDC2_2->setParent(ui->frame_4);
    //    buttonDC2_2->setGeometry(470,20,100,40);
    //    buttonDC2_2->setStyleSheet("QPushButton{border-style:none; background-image: url(:Res/image/unpressed.png);} QPushButton:pressed{background-image: url(:Res/image/pressed.png);}");
    //    buttonDC2_2->setFocusPolicy(Qt::NoFocus);
    //    buttonDC2_2->hide();

    //    buttonDC3_3 = new QPushButton(this);
    //    buttonDC3_3->setParent(ui->tab2frame);
    //    buttonDC3_3->setGeometry(470,20,100,40);
    //    buttonDC3_3->setStyleSheet("QPushButton{border-style:none; background-image: url(:Res/image/unpressed.png);} QPushButton:pressed{background-image: url(:Res/image/pressed.png);}");
    //    buttonDC3_3->setFocusPolicy(Qt::NoFocus);
    //    buttonDC3_3->hide();
#if(PRODUCER == 3 && SHOW_EXPENSE == 0)
    ui->tabWidget->removeTab(2);
    ui->tabWidget->setStyleSheet("QTabWidget::pane{background:rgb(126,205,244);border-style:none;border-width: 5px;border-color: rgb(13, 50, 109);border-radius:8px;}QTabBar::tab{width: 90px;height:87px;background : transparent;background-color:rgb(126,205,244);}QTabBar::tab:first:selected{border-image: url(:/Res/image/charge_watch_pressed.png);} QTabBar::tab:first:!selected{border-image: url(:/Res/image/charge_watch_unpressed.png);}QTabBar::tab:last:!selected{border-image: url(:/Res/image/battery_info_unpressed.png);}QTabBar::tab:last:selected{border-image: url(:/Res/image/battery_info_pressed.png);}");
    //    ui->buttonDC1->setStyleSheet("QPushButton{background-color: transparent;border-color: rgb(126, 205, 244);}");
    //    ui->buttonDC2->setStyleSheet("QPushButton{background-color: transparent;border-color: rgb(126, 205, 244);}");;
    //    ui->buttonDC3->setStyleSheet("QPushButton{background-color: transparent;border-color: rgb(126, 205, 244);}");;
#endif
#if ( PW_PUSHBUTTON == 1 )
    QPushButton* buttonKWChange = new QPushButton(ui->frameTop);
    buttonKWChange->setGeometry(340, 12, 120, 50);
    buttonKWChange->setStyleSheet("QPushButton{outline: none; font-size: 20px;  background-color: rgb(2, 45, 112); font-weight: bold; color: white;}");
    buttonKWChange->setText(tr("功率调节"));
    buttonKWChange->setFocusPolicy(Qt::NoFocus);
    connect(buttonKWChange, SIGNAL(clicked()), this, SLOT(onShorKeyboard()));
#endif
    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(HintStop_Button(int)));
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        connect(&Refreash_Thread[i], SIGNAL(StopingCharging(int)), this, SLOT(StopingChargingSlot(int)));//正在停止
        connect(&Refreash_Thread[i], SIGNAL(onStartingDeviceFail(int)), this, SLOT(onStartingDeviceFailSlot(int)));//启动失败
        connect(&Refreash_Thread[i], SIGNAL(onStartingDevice(int)), this, SLOT(onStartingDeviceSlot(int)));//正在启动
        connect(&Refreash_Thread[i], SIGNAL(onCharging(int)), this, SLOT(onChargingSlot(int)));//正在充电
    }
    connect(&mt, SIGNAL(sendData(QString,int,uint)), this, SLOT(SwipCardStop(QString,int,uint)));
}

ChargeWatchingDC::~ChargeWatchingDC()
{
    delete ui;
}

void ChargeWatchingDC::HintStop_Button(int current)
{
    if(current == chargeWatchingDCPage)
    {
        ui->_focus_gun_->setText(QString::number(focus_gun_));
        Button_Hide();
        Clear_OldInfo();
        //        ui->m_editDCChargeStatus->setText(tr("正在启动")); //充电状态;
        //        ui->labelHintDC->setText(QString(tr("%1号枪正在启动...")).arg(focus_gun_));
    }
}

void ChargeWatchingDC::Clear_OldInfo()
{
    ui->m_editDCChargeSOCWatch->setText("0"); //SOC
    ui->m_editDCChargeVoltage->setText("0.0"); //充电电压;

    //ui->m_editDCChargeTime->setText("0"); //充电时间;
    ui->ChargingTimeHourEdit->setText("0");
    ui->ChargingTimeMinEdit->setText("0");
    ui->ChargingTimeSecondEdit->setText("0");

    ui->m_editDCChargeCapacity->setText("0.00"); //充电电量;
    ui->m_editDCChargeCurrent->setText("0.00"); //充电电流;
    ui->m_editDCChargeTimeRemain->setText("0"); //剩余时间;

    //电池信息
    ui->m_editDCChargeSOCWatch_2->setText("0"); //SOC
    ui->m_editDCDemandVoltage->setText("0.0");//需求电压
    ui->m_editChargingMaxValLabel->setText("0.00");//单体最高电压
    ui->m_editBatteryMaxTemp->setText("0.0");//电池最高温度;
    ui->m_editNeedCur->setText("0.00");//需求电流;
    ui->m_editChargingMinLabel->setText("0.00");//单体最低电压;
    ui->m_editBatteryMinTemp->setText("0.0");//电池最低温度;
    //费用信息
    ui->m_editDCChargeSOCWatch_3->setText("0"); //SOC
    ui->m_editCurrentMoneyRate->setText("0.0000"); //当前电费单价
    ui->m_editChargingAllChargingMoney->setText("0.00"); //累计充电电费
    ui->m_editAllChargingMoneyLabel->setText("0.00"); //累计充电金额
    ui->m_editCurrentServiceRateLabel->setText("0.0000"); //当前服务费单价
    ui->m_editChargingAllServiceLabel->setText("0.00"); //累计充电服务费;

}
void ChargeWatchingDC::SwipCardStop(QString CardNum, int value, uint availabelMoney)
{
    qDebug("读卡标志: value = %d\n", value);//读卡失败为1
    qDebug()<<"接收到的卡号是:"<<CardNum << ";  "<<tr("卡内余额是: ")<<availabelMoney;
    if(stackLayout->currentIndex() == chargeWatchingDCPage && focus_gun_ != 0 && value == 0)
    {
        if(Refreash_Thread[focus_gun_-1].ThisGun_SwipCard_Num == CardNum)
        {
            //结束充电请求
            RequestStopCharging();
        }
        else
        {
            //请刷本次提供充电卡
            ui->labelHintDC->setText(QString(tr("刷卡错误,请刷本次提供的充电卡...")));
        }
    }

}


void ChargeWatchingDC::StopingChargingSlot(int GUN_NO)
{
    if(GUN_NO == focus_gun_ - 1)
    {
        qDebug()<<"--------------charge type: "<<chargingData->cmd3[GUN_NO].state;
        ui->m_editDCChargeStatus->setText(tr("正在停止")); //充电状态;
        if(chargingData->cmd3[GUN_NO].state==0){//card charge
            if(card->errcode == 0)
                ui->labelHintDC->setText(QString(tr("%1号枪正在停止充电,请稍后...")).arg(focus_gun_));
            else
                ui->labelHintDC->setText(QString(tr("读卡器通讯异常,正在停止充电...")));
        }
        else{
            ui->labelHintDC->setText(QString(tr("%1号枪正在停止充电,请稍后...")).arg(focus_gun_));
        }
        //开始结算
    }
    qDebug("%d号枪充电正在停止\n",focus_gun_);
    //    protocol_exit_seek_card_status();
    emit Settle_Accounts(GUN_NO);
    //更新停止充电的枪的结算界面
}
void ChargeWatchingDC::onStartingDeviceFailSlot(int GUN_NO)
{
    if(GUN_NO == focus_gun_ - 1)
    {
        if(Refreash_Thread[GUN_NO].swipCardflag == 1)
        {
            ui->m_editDCChargeStatus->setText(tr("启动失败")); //充电状态;
            ui->labelHintDC->setText(QString(tr("%1号枪启动失败,请刷卡退出")).arg(focus_gun_));
            //开启刷卡停止
            if(Refreash_Thread[GUN_NO].swipCardflag == 1 && card->errcode == 0 && !mt.isRunning())
                mt.start();
        }
        else if(Refreash_Thread[GUN_NO].appcharge == 1 || Refreash_Thread[GUN_NO].VINcharge == 1)
        {
            ui->m_editDCChargeStatus->setText(tr("启动失败")); //充电状态;
            ui->labelHintDC->setText(QString(tr("%1号枪启动失败...")).arg(focus_gun_));
            emit Settle_Accounts(GUN_NO);
        }
    }
}

void ChargeWatchingDC::onStartingDeviceSlot(int GUN_NO)
{
    if(GUN_NO == focus_gun_ - 1)
    {
        ui->m_editDCChargeStatus->setText(tr("正在启动")); //充电状态;
        ui->labelHintDC->setText(QString(tr("%1号枪正在启动...")).arg(focus_gun_));
    }
}

void ChargeWatchingDC::handle_put_in_charge(int GUN_NO)
{

}

void ChargeWatchingDC::onChargingSlot(int GUN_NO)
{
    if(GUN_NO == focus_gun_ - 1)
    {
        ui->m_editDCChargeStatus->setText(tr("正在充电")); //充电状态;
        if(Refreash_Thread[GUN_NO].appcharge == 1 || Refreash_Thread[focus_gun_ - 1].VINcharge == 1)
        {
            ui->labelHintDC->setText(QString(tr("%1号枪正在充电,点击停止按钮停止充电...")).arg(focus_gun_));
        }
        else if(Refreash_Thread[GUN_NO].swipCardflag == 1)
        {
            ui->labelHintDC->setText(QString(tr("%1号枪正在充电,请刷卡停止充电...")).arg(focus_gun_));
        }
        else if(Refreash_Thread[GUN_NO].swipcard_YCT == 1)
        {
            ui->labelHintDC->setText(QString(tr("%1号枪正在充电,请刷羊城通停止充电...")).arg(focus_gun_));
        }
        //设置各项数据显示
        Show_Charging_Infomation(focus_gun_);
        //开启刷卡停止
        if(Refreash_Thread[GUN_NO].swipCardflag == 1)
        {
            if(card->errcode == 0 &&
                    chargingData->gunmsg[GUN_NO].charge_state == 1)
            {
                if((chargingData->cmd3[GUN_NO].state == 0) &&
                        (chargingData->gunmsg[GUN_NO].u2.dc.MX[2] > 0) &&
                        chargingData->gunmsg[GUN_NO].errcode[0] == 0 &&
                        Refreash_Thread[GUN_NO].charge_status == 1)
                {
                    mt.start();
                }
                else if((chargingData->gunmsg[GUN_NO].charge_state == 1 &&
                         chargingData->cmd3[GUN_NO].state == 0) &&
                        chargingData->gunmsg[GUN_NO].u2.dc.MX[2] == 0 &&
                        chargingData->gunmsg[GUN_NO].errcode[0] == 0 &&
                        Refreash_Thread[GUN_NO].charge_status ==  1)
                {
                    mt.start();
                }

            }
            else
            {
                protocol_exit_seek_card_status();
            }
        }
    }
}
void ChargeWatchingDC::Show_Charging_Infomation(int _focus_gun_)
{

    if(focus_gun_ == _focus_gun_)
    {
        int m_num = _focus_gun_ - 1;
        char tmp[64]={0};

        ui->m_editDCChargeSOCWatch->setText(QString::number(chargingData->gunmsg[m_num].u2.dc.MX[2])); //SOC
        //        ui->m_editDCChargeVoltage->setText(QString::number((chargingData->gunmsg[m_num].u2.dc.MX[0]*1.0)/10, 'f', 1)); //充电电压;
        sprintf(tmp,"%.1f",(chargingData->gunmsg[m_num].u2.dc.MX[0]*1.0)/10);
        // qDebug()<< "SetVoltage QStirng:" << chargerVoltage << ", sprintf:" << tmp;
        ui->m_editDCChargeVoltage->setText(tmp);
        //        ui->m_editDCChargeTime->setText(QString::number(chargingData->gunmsg[m_num].charge_duration, 'f', 0)); //充电时间;
        // ui->m_editDCChargeTime->setText(QString::number(chargingData->gunmsg[m_num].charge_duration)); //充电时间;
        sprintf(tmp, "%d", chargingData->gunmsg[m_num].charge_duration / 3600);
        ui->ChargingTimeHourEdit->setText(tmp);
        sprintf(tmp, "%d", (chargingData->gunmsg[m_num].charge_duration % 3600)/60);
        ui->ChargingTimeMinEdit->setText(tmp);
        sprintf(tmp, "%d", chargingData->gunmsg[m_num].charge_duration % 60);
        ui->ChargingTimeSecondEdit->setText(tmp);
#if (PRODUCER == 3)
        ui->m_editDCChargeCapacity->setText(QString::number(((chargingData->gunmsg[m_num].curkWh) * 1.0)/1000, 'f', 3)); //充电电量;
#else
        //        ui->m_editDCChargeCapacity->setText(QString::number(((chargingData->gunmsg[m_num].curkWh) * 1.0)/1000, 'f', 2)); //充电电量;
        sprintf(tmp,"%.3f",(chargingData->gunmsg[m_num].curkWh*1.0)/1000);
        ui->m_editDCChargeCapacity->setText(tmp);
#endif
        //qDebug()<<QString::number(((chargingData->gunmsg[m_num].curkWh) * 1.0)/100, 'f', 2)<<"<<<<<充电电量";
        //        ui->m_editDCChargeCurrent->setText(QString::number(((chargingData->gunmsg[m_num].u2.dc.MX[1]) * 1.0)/10, 'f', 2)); //充电电流;
        sprintf(tmp,"%.2f",(chargingData->gunmsg[m_num].u2.dc.MX[1] * 1.0)/10);
        ui->m_editDCChargeCurrent->setText(tmp);

        ui->m_editDCChargeTimeRemain->setText(QString::number(chargingData->gunmsg[m_num].u2.dc.MX[14])); //剩余时间;
        //电池信息
        ui->m_editDCChargeSOCWatch_2->setText(QString::number(chargingData->gunmsg[m_num].u2.dc.MX[2])); //SOC

        //        ui->m_editDCDemandVoltage->setText(QString::number((chargingData->gunmsg[m_num].u2.dc.MX[9]*1.0)/10, 'f', 1));//需求电压
        sprintf(tmp,"%.1f",(chargingData->gunmsg[m_num].u2.dc.MX[9] * 1.0)/10);
        ui->m_editDCDemandVoltage->setText(tmp);

        //        ui->m_editChargingMaxValLabel->setText(QString::number((chargingData->gunmsg[m_num].u2.dc.MX[5]*1.0)/100, 'f', 2));//单体最高电压
        sprintf(tmp,"%.2f",(chargingData->gunmsg[m_num].u2.dc.MX[5] * 1.0)/100);
        ui->m_editChargingMaxValLabel->setText(tmp);

        //        ui->m_editBatteryMaxTemp->setText(QString::number(((chargingData->gunmsg[m_num].u2.dc.MX[4])*1.0), 'f', 1));//电池最高温度;
        sprintf(tmp,"%.1f",(chargingData->gunmsg[m_num].u2.dc.MX[4] * 1.0));
        ui->m_editBatteryMaxTemp->setText(tmp);

        //        ui->m_editNeedCur->setText(QString::number(((chargingData->gunmsg[m_num].u2.dc.MX[10])*1.0)/10, 'f', 2));//需求电流;
        sprintf(tmp,"%.2f",(chargingData->gunmsg[m_num].u2.dc.MX[10] * 1.0)/10);
        ui->m_editNeedCur->setText(tmp);

        //        ui->m_editChargingMinLabel->setText(QString::number((chargingData->gunmsg[m_num].u2.dc.MX[6]*1.0)/100, 'f', 2));//单体最低电压;
        sprintf(tmp,"%.2f",(chargingData->gunmsg[m_num].u2.dc.MX[6] * 1.0)/100);
        ui->m_editChargingMinLabel->setText(tmp);

        //        ui->m_editBatteryMinTemp->setText(QString::number(((chargingData->gunmsg[m_num].u2.dc.MX[3])*1.0), 'f', 1));//电池最低温度;
        sprintf(tmp,"%.1f",(chargingData->gunmsg[m_num].u2.dc.MX[3] * 1.0));
        ui->m_editBatteryMinTemp->setText(tmp);

        //费用信息
        ui->m_editDCChargeSOCWatch_3->setText(QString::number((chargingData->gunmsg[m_num].u2.dc.MX[2]))); //SOC
        //        ui->m_editCurrentMoneyRate->setText(QString::number(( chargingData->gunmsg[m_num].rate * 1.0)/10000, 'f', 4)); //当前电费单价
        sprintf(tmp,"%.4f",(chargingData->gunmsg[m_num].rate * 1.0)/10000);
        ui->m_editCurrentMoneyRate->setText(tmp);

        //        ui->m_editChargingAllChargingMoney->setText(QString::number(( chargingData->gunmsg[m_num].cost_elec * 1.0)/100, 'f', 2)); //累计充电电费
        sprintf(tmp,"%.2f",(chargingData->gunmsg[m_num].cost_elec * 1.0)/100);
        ui->m_editChargingAllChargingMoney->setText(tmp);

        //        ui->m_editAllChargingMoneyLabel->setText(QString::number(( chargingData->gunmsg[m_num].curcostmoney *1.0)/100, 'f', 2)); //累计充电金额
        sprintf(tmp,"%.2f",(chargingData->gunmsg[m_num].curcostmoney * 1.0)/100);
        ui->m_editAllChargingMoneyLabel->setText(tmp);

        //        ui->m_editCurrentServiceRateLabel->setText(QString::number(( chargingData->gunmsg[m_num].rate_server * 1.0)/10000, 'f', 4)); //当前服务费单价
        sprintf(tmp,"%.4f",(chargingData->gunmsg[m_num].rate_server * 1.0)/10000);
        ui->m_editCurrentServiceRateLabel->setText(tmp);

        //        ui->m_editChargingAllServiceLabel->setText(QString::number(( chargingData->gunmsg[m_num].cost_server * 1.0 )/100, 'f', 2)); //累计充电服务费;
        sprintf(tmp,"%.2f",(chargingData->gunmsg[m_num].cost_server * 1.0)/100);
        ui->m_editChargingAllServiceLabel->setText(tmp);
    }
}
void ChargeWatchingDC::onShorKeyboard()
{
    dlg->show();
}

void ChargeWatchingDC::Button_Hide()
{
    if(Refreash_Thread[focus_gun_-1].VINcharge == 0)
    {
        ui->StopVINButton1->hide();
        ui->StopVINButton2->hide();
        ui->StopVINButton3->hide();
    }
    else if(Refreash_Thread[focus_gun_-1].VINcharge == 1)
    {
        ui->StopVINButton1->show();
        ui->StopVINButton2->show();
        ui->StopVINButton3->show();
    }

    if(Refreash_Thread[focus_gun_-1].swipCardflag == 1)
    {
        ui->StopVINButton1->hide();
        ui->StopVINButton2->hide();
        ui->StopVINButton3->hide();
        ui->buttonDC1->hide();
        ui->buttonDC2->hide();
        ui->buttonDC3->hide();
    }
    if(Refreash_Thread[focus_gun_-1].appcharge == 1)
    {
        ui->buttonDC1->show();
        ui->buttonDC2->show();
        ui->buttonDC3->show();
    }
    else if(Refreash_Thread[focus_gun_-1].appcharge == 0)
    {
        ui->buttonDC1->hide();
        ui->buttonDC2->hide();
        ui->buttonDC3->hide();
    }
}
void ChargeWatchingDC::appStopbutton()
{
    if(chargingData->gunmsg[focus_gun_ - 1].charge_state == 1)
    {
        GUN_STATE[focus_gun_ - 1].Page = endOfChargeVerifyPage;
    }
}

void ChargeWatchingDC::on_buttonDC1_clicked()
{
    appStopbutton();
}

void ChargeWatchingDC::on_pushButton_clicked()
{
    Init_Focus();
}

void ChargeWatchingDC::on_buttonDC2_clicked()
{
    appStopbutton();
}

void ChargeWatchingDC::on_buttonDC3_clicked()
{
    appStopbutton();
}
void ChargeWatchingDC::Off_lineSlot()
{
    //    ui->buttonDC1->setStyleSheet("QPushButton{border-style:none; background-image: url(:Res/image/unpressed.png);} QPushButton:pressed{background-image: url(:Res/image/pressed.png);}");
    //    ui->buttonDC2->setStyleSheet("QPushButton{border-style:none; background-image: url(:Res/image/unpressed.png);} QPushButton:pressed{background-image: url(:Res/image/pressed.png);}");
    //    ui->buttonDC3->setStyleSheet("QPushButton{border-style:none; background-image: url(:Res/image/unpressed.png);} QPushButton:pressed{background-image: url(:Res/image/pressed.png);}");
}
void ChargeWatchingDC::On_lineSlot()
{
    //    ui->buttonDC1->setStyleSheet("QPushButton{background-color: transparent;border-color: rgb(126, 205, 244);}");
    //    ui->buttonDC2->setStyleSheet("QPushButton{background-color: transparent;border-color: rgb(126, 205, 244);}");;
    //    ui->buttonDC3->setStyleSheet("QPushButton{background-color: transparent;border-color: rgb(126, 205, 244);}");;
}

void ChargeWatchingDC::on_StopVINButton1_clicked()
{
    RequestStopCharging();
}

void ChargeWatchingDC::on_StopVINButton2_clicked()
{
    RequestStopCharging();
}

void ChargeWatchingDC::on_StopVINButton3_clicked()
{
    RequestStopCharging();
}

void ChargeWatchingDC::RequestStopCharging()
{
    // 结束充电请求
    chargingData->cmd2[focus_gun_-1].result = 0;
    protocol_exit_seek_card_status();
    Refreash_Thread[focus_gun_-1].Charging_Monitoring.stop();
    ui->m_editDCChargeStatus->setText(tr("正在停止"));
    ui->labelHintDC->setText(QString(tr("%1号枪正在停止充电,请稍后...")).arg(focus_gun_));
    chargingData->cmd4[focus_gun_-1].guntab = focus_gun_;
    chargingData->cmd4[focus_gun_-1].req_state = 1;
    emit Settle_Accounts(focus_gun_-1);
}
